package com.odobenus.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.odobenus.common.JsonObjectBase;
import com.odobenus.common.WebConstants;
import com.odobenus.service.ConfigFetchMgr;
import com.odobenus.vo.ConfForm;
import com.odobenus.vo.Config;

/**
 * 配置获取Controller, Disconf-client专门使用的
 *
 * @author liaoqiqi
 * @version 2014-6-16
 */
@Controller
@RequestMapping(WebConstants.API_PREFIX + "/web/config")
public class ConfigFetcherController extends BaseController {

    protected static final Logger LOG = LoggerFactory.getLogger(ConfigFetcherController.class);

    @Autowired
    private ConfigFetchMgr configFetchMgr;

    /**
     * 获取指定app env version 的配置项列表
     *
     * @param confForm
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonObjectBase getList(ConfForm confForm) {
        return getListImp(confForm, true);
    }


    private JsonObjectBase getListImp(ConfForm confForm, boolean hasValue) {
        LOG.info(confForm.toString());

        List<Config> configs =
                configFetchMgr.getConfListByParameter(confForm.getApp(), confForm.getEnv(),
                		confForm.getVersion(), hasValue);
        return buildListSuccess(configs, configs.size());
    }

}
